package com.hwadee.property.aspect;

import com.hwadee.property.pojo.ChargeDetail;
import com.hwadee.property.service.IChargeDetailService;
import com.hwadee.property.service.IUserActivityLogService;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class AliPayAspect {
    @Autowired
    private IUserActivityLogService logService; // 日志服务类

    @Autowired
    private IChargeDetailService chargeDetailService;

    // 定义切点，匹配 AliPayController 包下的所有方法
    @Pointcut("execution(* com.hwadee.property.controller.charge.AliPayController.*(..))")
    public void controllerMethods() {}

    // 在方法执行后执行日志记录
    @AfterReturning(pointcut = "controllerMethods()")
    public void logActivity(JoinPoint joinPoint) {
        // 获取目标方法的Method对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        // 获取@ApiOperation注解的value属性值
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);

        // 获取方法的参数名
        String[] parameterNames = signature.getParameterNames();
        // 获取方法的参数值
        Object[] args = joinPoint.getArgs();

        // 初始化日志参数
        Long chargeDetailId = null; // 缴费的条目
        StringBuilder relatedRecordIDBuilder = null; // 使用StringBuilder优化拼接, 关联记录为收费项目的id
        // 遍历参数名和参数值，匹配chargeDetailId
        for (int i = 0; i < parameterNames.length; i++) {
            String paramName = parameterNames[i];
            Object paramValue = args[i];

            // 匹配缴费条目参数
            if ("chargeDetailId".equals(paramName)) {
                chargeDetailId = (Long) paramValue;
            }

            // 匹配后缀为ID或Id的参数
            if (paramName.endsWith("ID") || paramName.endsWith("Id")) {
                if (relatedRecordIDBuilder == null) {
                    relatedRecordIDBuilder = new StringBuilder(paramName + ":" + paramValue);
                } else {
                    relatedRecordIDBuilder.append(", ").append(paramName).append(":").append(paramValue);
                }
            }
        }
        // 将StringBuilder转换为字符串
        String relatedRecordID = relatedRecordIDBuilder != null ? relatedRecordIDBuilder.toString() : null;

        Long userId = null;
        Long communityID = null;
        String content = "【AliPay】支付参数回调";
        Double amount = null;
        if (chargeDetailId != null){
            ChargeDetail chargeDetail = chargeDetailService.getById(chargeDetailId);
            userId = chargeDetail.getOwnerId();
            communityID = chargeDetail.getCommunityId();
            content = "【用户】缴纳物业费用";
            amount = chargeDetail.getAmount();
        }

        String logType = "【缴费】AliPay";

        // 调用日志保存方法
        logService.saveActivityLog(userId, communityID, content, amount, logType, relatedRecordID);
    }

}
